#main.py
#Timothy Sassone
#Provides a command-line interface to
#dice-rolling utilities.

from test import test #Used to perform dice rolls
import re #Necessary for regex testing
from tag import * #Used to store character data
from commands import * #Used to parse and handle commands


#Print the instructions once at startup.
print("Type a command to perform a test, or type \"quit\" to quit.")
print("Type \"help\" for a list of valid commands and their arguments.")


#KEY VARIABLES
root = Tag(None, "root") #The root tag, used to contain all character data
roll = test() #A dice-roll/testing instance, to perform the commands
command = "" #Used to store the latest command


#WHILE LOOP BEGIN
while command != "quit": #repeat until they type quit
    command = input("cmd:>") #Get a command from the user
    print("\n") #print a newline for spacing purposes.

    #BEGIN COMMAND CHECKING
    if(command=="quit"): #If they're quitting,
        break #We don't want to do anything else in the loop
    
    elif(command=="help"): #If they entered the help command, print the cmd list.
        print("COMMANDS:")
        print("roll <pool>")
        print("  Rolls a dice pool of the given size.")
        print("  Ex: roll 10")
        print()
        print("success <pool> <threshold>")
        print("  Rolls a dice pool against a threshold, reporting the results.")
        print("  Ex: success 10 4")
        print()
        print("opposed <act> <opp> <group>")
        print("  Rolls an opposed test, potentially against a group.")
        print("  (Use 1 for group to roll against an individual)")
        print("  Ex: opposed 10 7 1")
        print()
        print("quickex <pool> <threshold> <interval>")
        print("  Rolls a quick-extended test.")
        print("  Ex: quickex 15 25 30 minutes")
        print()
        print("fullex <pool> <threshold> <interval> <group> <limit>")
        print("  Rolls  a full-extended test.")
        print("  (Use 1 for group to roll against individual)")
        print("  (Unit of time is only entered for interval, not limit)")
        print("  Ex: fullex 15 25 30 minutes 1 120")
        
    elif(re_legal_load.match(command)): #If the command is a load command
        parts = command.split(" ") #Split the command by spaces
        fname = parts[1] #The second part is the filename (first is "load")
        root.load_tags(fname + ".txt") #Try to load that file.

    elif(re_legal_view.match(command)): #If the command is a view command
        parts = command.split(" ") #split the command by spaces
        name = parts[1] #The second part is the name (first is "view")
        tags = [] #Will end up holding the character tags
        if(name=="all"): #If the user wants all of them
            tags = root.find_tags("character") #select them all
        else: #otherwise
            tags = root.find_tags("character:"+name) #select only those where the name matches
        if(tags!=[]): #If we didn't get an empty set
            for t in tags: #Loop through the tags
                t.show_tree() #Print each one as a tree
        else: #If we did get an empty set
            if(name=="all"): #if they ask for all characters 
                print("No characters loaded!") #Tell them there are none
            else: #otherwise
                print("Character \"" + name + "\" not found!") #That specific character is missing

    #CHECK FOR A ROLL COMMAND
    elif(re_legal_roll.match(command)):
        parts = command.split(" ") #Split the command by spaces
        pool = eval_expression(parts[1], root) #Evaluate the expression
        roll.roll(pool) #Roll that many dice

    #Other command handling works as roll command,
    #so I'm not going to bother repeating the comments.
    elif(re_legal_success.match(command)):
        parts = command.split(" ")
        pool = eval_expression(parts[1], root)
        threshold = eval_expression(parts[2], root)
        roll.success(pool, threshold)
        
    elif(re_legal_opposed.match(command)):
        parts = command.split(" ")
        act = eval_expression(parts[1], root)
        opp = eval_expression(parts[2], root)
        grp_add = eval_expression(parts[3], root)
        roll.opposed(act, opp, grp_add-1)
        
    elif(re_legal_quickex.match(command)):
        parts = command.split(" ")
        pool = eval_expression(parts[1], root)
        threshold = eval_expression(parts[2], root)
        interval_len = eval_expression(parts[3], root)
        interval_unit = parts[4] #This one is a string (I.E. "minutes"),
                                 #and so shouldn't be evaluated.
        roll.quick_etest(pool, threshold, interval_len, interval_unit)
        
    elif(re_legal_fullex.match(command)):
        parts = command.split(" ")
        pool = eval_expression(parts[1], root)
        threshold = eval_expression(parts[2], root)
        interval_len = eval_expression(parts[3], root)
        interval_unit = parts[4] #See quickex test
        group = eval_expression(parts[5], root)
        limit = eval_expression(parts[6], root)
        roll.single_etest(pool, threshold, interval_len,
                          interval_unit, group-1, limit)
        
    else: #Nothing has matched...
        print("Invalid input, please try again.")
        print("Type \"help\" for a list of valid commands and their arguments.")
    #END COMMAND CHECKING
        
    print("\n")
#END WHILE LOOP
